﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace Remind.View
{
    class FileOperator
    {
        //判断是否是隐藏文件
        public bool IsHidden(string path)
        {
            FileAttributes myAttributes = File.GetAttributes(path);
            string myFileType = myAttributes.ToString();
            if (myFileType.LastIndexOf("Hidden") != -1)
                return true;
            else
                return false;
        }
        //创建指定目录
        public void CreateDirectory(string targetDir)
        {
            DirectoryInfo dir = new DirectoryInfo(targetDir);
            if (!dir.Exists)
                dir.Create();
        }
        //删除文件或目录
        public void DeleteFiles(string targetDir, bool deSubDir)
        {
            foreach (string fileName in Directory.GetFiles(targetDir))    
            {
                File.SetAttributes(fileName, FileAttributes.Normal);
                File.Delete(fileName);
            }
            if (deSubDir)
            {
                DirectoryInfo dir = new DirectoryInfo(targetDir);
                foreach (DirectoryInfo subDir in dir.GetDirectories())
                {
                    DeleteFiles(subDir.FullName, true);
                    subDir.Delete();
                }
            }
        }
        //删除指定目录的所有子目录，不包括对当前目录文件的删除
        public void DeleteSubDirectory(string targetDir)
        {
            foreach (string subDir in Directory.GetDirectories(targetDir))
            {
                DeleteDirctory(subDir);
            }
        }

        //删除指定目录，包括当前目录和所有子目录和文件
        public void DeleteDirctory(string targetDir)
        {
            DirectoryInfo dirInfo = new DirectoryInfo(targetDir);
            if (dirInfo.Exists)
            {
                DeleteFiles(targetDir, true);
                dirInfo.Delete(true);
            }
        }
        /// <summary>
        /// 移动（剪切）文件或目录
        /// </summary>
        /// <param name="sourceDir">原始目录</param>
        /// <param name="targetDir">目标目录</param>
        /// <param name="overWrite">true：覆盖同文目录</param>
        /// <param name="moveSubDir">true:包含目录</param>
        public void MoveFiles(string sourceDir, string targetDir, bool overWrite, bool moveSubDir)
        {
            //移动当前目录文件
            foreach (string sourceFileName in Directory.GetFiles(sourceDir))
            {
                string targetFileName = Path.Combine(targetDir, sourceFileName.Substring(sourceFileName.LastIndexOf("\\") + 1));
                if (File.Exists(targetFileName))
                {
                    if (overWrite)
                    {
                        File.SetAttributes(targetFileName, FileAttributes.Normal);
                        File.Delete(targetFileName);
                        File.Move(sourceFileName, targetFileName);
                    }
                }
                else
                {
                    File.Move(sourceFileName, targetFileName);
                }
            }
            if (moveSubDir)
            {
                foreach (string sourceSubDir in Directory.GetDirectories(sourceDir))
                {
                    string targetSubDir = Path.Combine(targetDir, sourceSubDir.Substring(sourceSubDir.LastIndexOf("\\") + 1));
                    if (!Directory.Exists(targetSubDir))
                        Directory.CreateDirectory(targetSubDir);
                    MoveFiles(sourceSubDir, targetSubDir, overWrite, true);
                    Directory.Delete(sourceSubDir);    
                }
            }
        }

        /// <summary>
        /// 复制文件或目录
        /// </summary>
        /// <param name="sourceDir">原始目录</param>
        /// <param name="targetDir">目标目录</param>
        /// <param name="overWrite">true:覆盖同名文件</param>
        /// <param name="copySubDir">true:包含目录</param>
        public void CopyFiles(string sourceDir, string targetDir, bool overWrite, bool copySubDir)
        { 
            //复制当前目录文件
            foreach (string sourceFileName in Directory.GetFiles(sourceDir))
            {
                string targetFileName = Path.Combine(targetDir, sourceFileName.Substring(sourceFileName.LastIndexOf("\\") + 1));
                if (File.Exists(targetFileName))
                {
                    if (overWrite == true)
                    {
                        File.SetAttributes(targetFileName, FileAttributes.Normal);
                        File.Copy(sourceFileName, targetFileName, overWrite);
                    }
                }
                else
                {
                    File.Copy(sourceFileName, targetFileName, overWrite);
                }
            }
            //复制子目录
            if (copySubDir)
            {
                foreach (string sourceSubDir in Directory.GetFiles(sourceDir))
                {
                    string targetSubDir = Path.Combine(targetDir, sourceSubDir.Substring(sourceSubDir.LastIndexOf("\\") + 1));
                    if (!Directory.Exists(targetSubDir))
                        Directory.CreateDirectory(targetSubDir);
                    CopyFiles(sourceSubDir, targetSubDir, overWrite, true);
                }
            }
        }

        //获取子目录文件信息
        public void GetChildFileInfo(string targetDir)
        {
            try
            {
                foreach (string fileName in Directory.GetFiles(targetDir))
                {
                    FileInfo fileInfo = new FileInfo(fileName);
                    string name = fileName.Substring(fileName.LastIndexOf("\\") + 1);//how文件名
                    string address = fileName;//地址
                    string size = fileInfo.Length.ToString();//文件大小，
                    string createtime = fileInfo.CreationTime.ToString();//创建时间
                }
                foreach (string directory in Directory.GetDirectories(targetDir))
                {
                    //获取文件夹相关信息
                    GetChildFileInfo(directory);//递归调用

                }
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}
